<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="危废名称" prop="wasteName">
                <el-input v-model="queryParams.wasteName" placeholder="请输入危废名称" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="批次号码" prop="batchNumber">
                <el-input v-model="queryParams.batchNumber" placeholder="请输入批次号码" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label-width="100px" label="入库操作者" prop="entryOperator">
                <el-input v-model="queryParams.entryOperator" placeholder="请输入收运者" clearable size="small" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="入库日期">
                <el-date-picker v-model="daterangeEntryDate" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="危废仓库" prop="warehouseId">
                <el-select v-model="queryParams.warehouseId" placeholder="请选择仓库">
                    <el-option v-for="item in warehouseList" :label="item.warehouseName" :key="item.warehouseId" :value="item.warehouseId" />
                </el-select>
            </el-form-item>
            <el-form-item label="危废状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择危废状态">
                    <el-option v-for="dict in wasteFlowStatusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['business:wasteInventory:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="wasteInventoryList" @selection-change="handleSelectionChange">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="主键id" align="center" prop="wasteInventoryId" v-if="false" />
            <el-table-column label="危废收运ID" align="center" prop="wasteCollectId" v-if="false" />
            <el-table-column label="批次号码" align="center" prop="batchNumber" width="180" />
            <el-table-column label="状态" align="center" prop="status" width="100" :formatter="wasteFlowStatusFormat" />
            <el-table-column type="expand">
                <template slot-scope="props">
                    <keep-alive>
                        <waste-info-card :row="props.row" />
                    </keep-alive>
                </template>
            </el-table-column>
            <el-table-column label="危废名称" align="left" prop="wasteName" />
            <el-table-column label="仓库名称" align="center" prop="warehouseName" />
            <el-table-column label="入库数量" align="center" prop="wasteWeight" />
            <el-table-column label="库存数量" align="center" prop="stockWeight" />
            <el-table-column label="存放天数" align="center" prop="stockDay" />
            <el-table-column label="入库日期" align="center" prop="entryDate" width="180" />
            <el-table-column label="入库操作者" align="center" prop="entryOperator" width="100" />
            <el-table-column label="出库日期" align="center" prop="outDate" width="180" />
            <el-table-column label="出库操作者" align="center" prop="outOperator" width="100" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.hasAttachment === true" size="mini" type="text" icon="el-icon-view" @click="handleOpenAttachmentModal(scope.row)" v-hasPermi="['business:wasteDeclare:add']">查看附件</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改危废库存信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 查看附件 -->
        <el-dialog title="查看附件" :visible.sync="openAttachmentModal" width="500px" append-to-body>
            <el-form ref="form" :model="attachmentForm" label-width="80px">
                <el-form-item label="危废名称" prop="wasteId">
                    <el-input v-model="attachmentForm.wasteName" disabled />
                </el-form-item>
                <el-form-item label="附件">
                    <MultiFileUpload v-model="attachmentForm.fileList" :disabled="true" ListType="text" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelAttachmentModal">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listWasteInventory, getWasteInventory, delWasteInventory, addWasteInventory, updateWasteInventory, exportWasteInventory } from "@/api/business/wasteInventory";
import { listWarehouseForSelect } from "@/api/system/warehouse";
import WasteInfoCard from "@/views/business/wasteInfoCard";
import MultiFileUpload from "@/components/MultiFileUpload";

import { mapActions, mapGetters } from "vuex";

export default {
    name: "WasteInventory",
    components: {
        WasteInfoCard,
        MultiFileUpload
    },
    data() {
        return {
            //按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 危废库存信息表格数据
            wasteInventoryList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                wasteId: undefined,
                warehouseId: undefined,
                wasteName: undefined,
                batchNumber: undefined,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            },
            // 入库日期范围
            daterangeEntryDate: [],
            // 仓库列表
            warehouseList: [],
            // 附件表单
            attachmentForm: {},
            // 附件表单是否打开
            openAttachmentModal: false,
        };
    },
    created() {
        this.getList();
        this.initDictData();
        this.getWarehouseList();
    },
    computed: {
        ...mapGetters(["wasteFlowStatusOptions"]),
    },
    methods: {
        ...mapActions({
            initDictData: "initDataForWasteInfoCard",
        }),
        // 获取仓库列表
        getWarehouseList() {
            listWarehouseForSelect().then(response => {
                this.warehouseList = response.data;;
            });
        },
        /** 查询危废库存信息列表 */
        getList() {
            this.loading = true;
            this.queryParams.params = {};
            if (null != this.daterangeEntryDate && '' != this.daterangeEntryDate) {
                this.queryParams.params["beginEntryDate"] = this.daterangeEntryDate[0];
                this.queryParams.params["endEntryDate"] = this.daterangeEntryDate[1];
            }
            listWasteInventory(this.queryParams).then(response => {
                this.wasteInventoryList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 危废状态字典翻译
        wasteFlowStatusFormat(row, column) {
            return this.selectDictLabel(
                this.wasteFlowStatusOptions,
                row.status
            );
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                wasteInventoryId: undefined,
                wasteId: undefined,
                warehouseId: undefined,
                entryWarehouseDetailId: undefined,
                wasteName: undefined,
                batchNumber: undefined,
                wasteWeight: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            daterangeEntryDate: [],
                this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.wasteInventoryId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加危废库存信息";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.loading = true;
            this.reset();
            const wasteInventoryId = row.wasteInventoryId || this.ids
            getWasteInventory(wasteInventoryId).then(response => {
                this.loading = false;
                this.form = response.data;
                this.open = true;
                this.title = "修改危废库存信息";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.buttonLoading = true;
                    if (this.form.wasteInventoryId != null) {
                        updateWasteInventory(this.form).then(response => {
                            this.buttonLoading = false;
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addWasteInventory(this.form).then(response => {
                            this.buttonLoading = false;
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const wasteInventoryIds = row.wasteInventoryId || this.ids;
            this.$confirm('是否确认删除危废库存信息编号为"' + wasteInventoryIds + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                this.loading = true;
                return delWasteInventory(wasteInventoryIds);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$confirm('是否确认导出所有危废库存信息数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                this.exportLoading = true;
                return exportWasteInventory(queryParams);
            }).then(response => {
                this.download(response.msg);
                this.exportLoading = false;
            }).catch(() => { });
        },
        /** 查看附件 */
        handleOpenAttachmentModal(row) {
            let { wasteName, fileList } = row;
            fileList = !fileList ? [] : fileList;
            this.attachmentForm = { wasteName, fileList }
            this.openAttachmentModal = true;
        },

        // 附件表单关闭
        cancelAttachmentModal() {
            this.openAttachmentModal = false;
        },
    }
};
</script>
<style>
.demo-table-expand {
    font-size: 0;
    padding: 0px 0px 0px 150px;
}
.demo-table-expand label {
    width: 90px;
    color: #99a9bf;
}
.demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
}
</style>